package messages;

import util.ByteList;
import util.Utils;

public class DisconnectReq extends MessageHeader {

	public static final short	DISCONNECT_REQ_ID	= 7;

	/* ---------------------------------------------------------------- CONSTRUCTORS */

	/**
	 * @param _destID
	 * @param _srcID
	 */
	public DisconnectReq( short _destID , short _srcID ) {
		super( _destID , _srcID );
		concatCommandID( DISCONNECT_REQ_ID );
	}

	/* ---------------------------------------------------------------- PRIVATE */

	/**
	 * @param commandID
	 */
	private void concatCommandID( short commandID ) {
		this.incrementLength( (short) 2 );
	}

	/* ---------------------------------------------------------------- PUBLIC */

	@Override
	public String toString( ) {
		return " [ DISCONNECT_REQ ] = " + super.toString( ) + " | <Command_ID=" + DISCONNECT_REQ_ID
				+ "> | ";
	}

	@Override
	public ByteList assembleMsg( ) {
		ByteList bList = super.assembleMsg( );
		int len = bList.size( );

		bList.concat( Utils.shortToBytes( Utils.reverseEndianess_16bits( DISCONNECT_REQ_ID ) ) );

		// insert escape byte 'ffh' after '1bh' bytes
		for ( int i = len ; i < bList.size( ) ; i++ )
			if ( bList.get( i ) == 0x1b ) {
				if ( ( i + 1 ) == bList.size( ) )
					bList.add( Utils.ESCAPE_CHAR );
				else
					bList.add( Utils.ESCAPE_CHAR );
			}

		return bList;
	}
}
